package co.fitstart.scheduler.handler.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.support.Result;
import com.demo2do.core.support.sms.handler.SmsHandler;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.scheduler.entity.sms.MessageType;
import co.fitstart.scheduler.handler.SmsSender;
import co.fitstart.scheduler.handler.message.WeappMessageHandler;


/**
 * 
 * @author haiyan_xiao
 *
 */
@Component("smsSender")
public class SmsSenderImpl implements SmsSender {
    
    private static final Logger logger = LoggerFactory.getLogger(SmsSender.class);
    
    @Autowired
    private SmsHandler smsHandler;
    
    @Autowired
    private WeappMessageHandler weappMessageHandler;

    @Value("#{config['sms.company']}")
    private String company;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.scheduler.handler.SmsSender#sendTwoWeeksLaterExpireNotice(java.lang.String, java.lang.String, int, java.lang.String, java.util.Date)
     */
    public void sendTwoWeeksLaterExpireNotice(String mobile, String site, int courseType, String courseAmount,
            Date effectiveEnd) {
        Result result = new Result();
        
        // Get message template
        String template = smsHandler.getShortMessageTemplate(MessageType.TWO_WEEKS_LATER_EXPIRE_NOTICE);
        
        if (template != null) {
            
            Map<String, String> values = new HashMap<String, String>(2);
            values.put("company", company);
            values.put("site", site);
            values.put("courseType", courseType == 1 ? ("1V1课程" + courseAmount + "节") : "年卡课程");
            values.put("effectiveEnd", DateFormatUtils.format(effectiveEnd, "yy/MM/dd"));
            
            String message = StringUtils.replacePlaceHolder(template, values, "#", "#");
            
            // Send message
            result = smsHandler.sendShortMessage(mobile, message);
            
            if(!result.isValid()) {
                logger.error("sendTwoWeeksLaterExpireNotice() error", JsonUtils.toJsonString(result));
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.scheduler.handler.SmsSender#sendTwoHoursLaterScheduleNotice(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public void sendTwoHoursLaterScheduleNotice(String openid, String courseType, String mobile, String site, String coach, String occurDate,
            String occurHour) {
        Result result = new Result();
        
        // Get message template
        String template = smsHandler.getShortMessageTemplate(MessageType.TWO_HOURS_LATER_SCHEDULE_NOTICE);
        
        if (template != null) {
            
            Map<String, String> values = new HashMap<String, String>(2);
            values.put("company", company);
            values.put("site", site);
            values.put("coach", coach);
            values.put("occurDate", occurDate);
            values.put("occurHour", occurHour);
            
            String message = StringUtils.replacePlaceHolder(template, values, "#", "#");

            // send dingyue message
            logger.info("get openid [{}]", openid);
            if(org.apache.commons.lang3.StringUtils.isNotEmpty(openid)) {
            	result = weappMessageHandler.sendDingyueMessage(openid, courseType, occurDate + " " + occurHour, coach, site);
            } else {
            	// Send message
                result = smsHandler.sendShortMessage(mobile, message);
            }
            
            if(!result.isValid()) {
                logger.error("sendTwoHoursLaterScheduleNotice() error", JsonUtils.toJsonString(result));
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.scheduler.handler.SmsSender#sendTenDaysLaterUnfreezeNotice(java.lang.String, java.lang.String, int, java.lang.Integer, java.util.Date)
     */
    public void sendTenDaysLaterUnfreezeNotice(String mobile, String site, int courseType, Integer restAmount,
            Date HoldEndDate) {
        Result result = new Result();
        
        // Get message template
        String template = smsHandler.getShortMessageTemplate(MessageType.TEN_DAYS_LATER_UNFREEZE_NOTICE);
        
        if (template != null) {
            
            Map<String, String> values = new HashMap<String, String>(2);
            values.put("company", company);
            values.put("site", site);
            values.put("courseType", courseType == 1 ? ("1V1课程剩余" + restAmount + "节") : "年卡课程");
            values.put("holdEndDate", DateFormatUtils.format(HoldEndDate, "yy/MM/dd"));
            
            String message = StringUtils.replacePlaceHolder(template, values, "#", "#");
            
            // Send message
            result = smsHandler.sendShortMessage(mobile, message);
            
            if(!result.isValid()) {
                logger.error("sendTenDaysLaterScheduleNotice() error", JsonUtils.toJsonString(result));
            }
        }
    }
}
